/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2007, 2008 University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <vector>
#include "layer2-p2mp-channel.h"
#include "layer2-p2p-net-device.h"
#include "ns3/trace-source-accessor.h"
#include "ns3/packet.h"
#include "ns3/simulator.h"
#include "ns3/log.h"
#include "ns3/net-device-container.h"

namespace ns3 {

NS_LOG_COMPONENT_DEFINE ("Layer2P2MPChannel");

NS_OBJECT_ENSURE_REGISTERED (Layer2P2MPChannel);

TypeId 
Layer2P2MPChannel::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::Layer2P2MPChannel")
    .SetParent<Channel> ()
    .SetGroupName ("Layer2P2P")
    .AddConstructor<Layer2P2MPChannel> ()
    .AddAttribute ("Delay", "Transmission delay through the channel",
                   TimeValue (Seconds (0)),
                   MakeTimeAccessor (&Layer2P2MPChannel::m_delay),
                   MakeTimeChecker ())
    .AddAttribute ("ReconfigureDelay",
                   "Changing the circuit configuration imposes a reconfiguration delay",
                   TimeValue (NanoSeconds (30)),
                   MakeTimeAccessor (&Layer2P2MPChannel::m_reconfigureDelay),
                   MakeTimeChecker ())
  ;
  return tid;
}

//
// By default, you get a channel that 
// has an "infitely" fast transmission speed and zero delay.
Layer2P2MPChannel::Layer2P2MPChannel()
  :
    Channel (),
    m_delay (Seconds (0.)),
    m_state (INITIALIZING),
    m_src (0)
{
  NS_LOG_FUNCTION_NOARGS ();
}

void
Layer2P2MPChannel::Attach (Ptr<Layer2P2PNetDevice> device)
{
  NS_LOG_FUNCTION (this << device);
  m_src = device;
  m_state = IDLE;
}

void
Layer2P2MPChannel::SetDstDevices (NetDeviceContainer devices)
{
  NS_LOG_FUNCTION (this);
  Ptr<NetDevice> netDevice;
  for (NetDeviceContainer::Iterator i = devices.Begin (); i != devices.End (); ++i)
    {
      netDevice = (*i);
      Ptr<Layer2P2PNetDevice> l2Device = DynamicCast<Layer2P2PNetDevice>(netDevice);
      if (l2Device)
        {
          m_dst.push_back (l2Device);
        }
    }
}

void
Layer2P2MPChannel::AddMask (uint16_t location)
{
  m_mask.push_back(location);
}

std::vector<uint16_t>
Layer2P2MPChannel::GetMask ()
{
  return m_mask;
}

void
Layer2P2MPChannel::ClearMask ()
{
  m_mask.clear();
}

bool
Layer2P2MPChannel::TransmitStart (
  Ptr<Packet> p,
  Ptr<Layer2P2PNetDevice> src,
  Time txTime)
{
  NS_LOG_FUNCTION (this << p << src);
  NS_LOG_LOGIC ("UID is " << p->GetUid () << ")");

  NS_ASSERT (m_state != INITIALIZING);
  NS_ASSERT (!m_mask.empty());

  Ptr<Packet> originalPacket = p->Copy ();

  //m_dst.size()>1 means physical layer broadcast
  for (std::vector<uint16_t>::const_iterator i = m_mask.begin(); i != m_mask.end(); i++)
  {
    NS_LOG_INFO ("tarnsmit dest !!!! " << *i);
    Simulator::ScheduleWithContext (m_dst.at(*i)->GetNode ()->GetId (),
                                  txTime + m_delay, &Layer2P2PNetDevice::Receive,
                                  m_dst.at(*i), originalPacket);
  }

  return true;
}

uint32_t 
Layer2P2MPChannel::GetNDevices (void) const
{
  NS_LOG_FUNCTION_NOARGS ();
  return m_mask.size();
}

Ptr<NetDevice>
Layer2P2MPChannel::GetDevice (uint32_t i) const
{
  NS_LOG_FUNCTION_NOARGS ();
  return m_src;
}

Ptr<NetDevice>
Layer2P2MPChannel::GetDstDevice (uint32_t location)
{
  NS_LOG_FUNCTION (this);
  return m_dst.at(location);
}

Time
Layer2P2MPChannel::GetDelay (void) const
{
  return m_delay;
}

bool
Layer2P2MPChannel::IsInitialized (void) const
{
  NS_ASSERT (m_state != INITIALIZING);
  return true;
}

} // namespace ns3
